import 'package:ai_data_capture/bean/ConfigFileBean.dart';
import 'package:ai_data_capture/req/api/ConfigApi.dart';
import 'package:ai_data_capture/req/api/CurrentConfigApi.dart';
import 'package:ai_data_capture/util/toast_util.dart';
import 'package:get/get.dart';

import 'state.dart';

class ConfigfileLogic extends GetxController {
  final ConfigfileState state = ConfigfileState();

  @override
  void onInit() {
    super.onInit();
    init();
  }

  Future<void> addConfigFile(ConfigFileBean r, {bool isEdit = false}) async {
    try {
      await ConfigApi.addConfigFile(r, isEdit: isEdit);
      showToast("提交成功");
    } catch (e) {
      showToast("提交失败$e");
    }
    init();
  }

  Future<void> removeFile(num? id) async {
    try {
      await ConfigApi.removeConfigFile(id);
      showToast("删除成功");
    } catch (e) {
      showToast("删除失败$e");
    }
    init();
  }

  Future<void> setupConfig(ConfigFileBean bean) async {
    try {
      await CurrentConfigApi.setupConfigFile(bean);
      showToast("设置成功");
    } catch (e) {
      showToast("设置失败$e");
    }
    init();
  }

  Future<void> init() async {
    state.list.clear();
    state.list.addAll(await ConfigApi.getConfigFile());
    state.currentConfig.value = await CurrentConfigApi.getCurrentConfig();
  }
}
